local CSGuildWarMatchInfo = class("CSGuildWarMatchInfo")
local CSGuildWarGuildInfo = import(".CSGuildWarGuildInfo")

function CSGuildWarMatchInfo:ctor()
    self._guild1 = nil
    self._guild2 = nil
    self._winner = nil
    self._status = 0
    self._id = 0
    self._error = nil
end

function CSGuildWarMatchInfo:read(pbData)
    if not pbData then
        return
    end
    self._guild1 = nil
    self._guild2 = nil
    local guild1Data = rawget(pbData, "guild1")
    if guild1Data then
        self._guild1 = CSGuildWarGuildInfo.new()
        self._guild1:read(guild1Data)
        self._guild1:setMatchIndex(1)
    end

    local guild2Data = rawget(pbData, "guild2")
    if guild2Data then
        self._guild2 = CSGuildWarGuildInfo.new()
        self._guild2:read(guild2Data)
        self._guild2:setMatchIndex(2)
    end

    self._winner = rawget(pbData, "winner")
    self._phase = rawget(pbData, "phase") or 0
    self._round = rawget(pbData, "round") or  0
    self._id = rawget(pbData, "id") or 0
    self._error  = rawget(pbData, "error")
end


function CSGuildWarMatchInfo:getError()
    if self._error then
        return L(self._error)
    end
end

function CSGuildWarMatchInfo:getId()
    return self._id
end

function CSGuildWarMatchInfo:getGuild1()
    return self._guild1
end

function CSGuildWarMatchInfo:getGuild2()
    return self._guild2
end

function CSGuildWarMatchInfo:getWinner()
    return self._winner
end

function CSGuildWarMatchInfo:getRound()
    return self._round
end


-- rank 字段表示 预选赛阶段的排名
-- _round 字段表示决赛阶段的 排名 
-- 返回 临时的 排名    第二返回  表示是否是真实排名
function CSGuildWarMatchInfo:getRealRank(guild)
    if not guild then
        return 0, false
    end
    local weekDay = app:getWeekDay()
    if weekDay == 6 then
        -- 决赛阶段
        return self._round * 4 + 1, false
    elseif weekDay == 7 then
           -- 决赛阶段
        if self._winner then
            if guild:getGuildId() == self._winner then
                return self._round * 2 + 1, true
            else
                return self._round * 2 + 2, true
            end
        else
            return self._round * 2 + 1, false
        end
    else
        -- 预选赛阶段
        return guild:getRank(), true
    end
end



function CSGuildWarMatchInfo:getPhase()
    return self._phase
end

function CSGuildWarMatchInfo:getGuildWithSort()
    if self._guild1 and self._guild2 then
        if self._guild1:isMyGuild() then
            return self._guild1, self._guild2
        elseif self._guild2:isMyGuild() then
            return self._guild2, self._guild1
        else
            local serverIndex1 = self._guild1:getServerIndex()
            local serverIndex2 = self._guild2:getServerIndex()
            if serverIndex1 == serverIndex2 then
                return self._guild1, self._guild2
            else
                local curServer = tonumber(app.session:getZoneId() or 0)
                if serverIndex1 == curServer then
                    return self._guild1, self._guild2
                elseif serverIndex2 == curServer then
                    return self._guild2, self._guild1
                else
                    if serverIndex1 < serverIndex2 then
                        return self._guild1, self._guild2
                    else
                        return self._guild2, self._guild1
                    end
                end
            end
        end
    else
        return self._guild1, self._guild2
    end
end

function CSGuildWarMatchInfo:isLeftWin()
    local guild1 = self:getGuildWithSort()
    if guild1 then
        return guild1:getGuildId() == self._winner
    end
end


return CSGuildWarMatchInfo